package com.example.baselibs.net;


import com.blankj.utilcode.util.NetworkUtils;

import java.net.ConnectException;
import java.net.NoRouteToHostException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

abstract class RxObserver<T> implements Observer<T> {

    @Override
    public void onSubscribe(Disposable d) {
        if (NetworkUtils.isConnected()){
            onStart("数据加载中");
        }else {
            onError(new ConnectException());
            d.dispose();
        }
    }

    @Override
    public void onNext(T t) {
        onSuccess(t);
    }

    @Override
    public void onError(Throwable e) {
        if (e instanceof ConnectException){
            onFailure("没有联网");
        }else if (e instanceof SocketTimeoutException){
            onFailure("链接超时");
        }else if (e instanceof NoRouteToHostException){
            onFailure("获取失败");
        }else if (e instanceof UnknownHostException){
            onFailure("无网络连接");
        }else {
            onFailure("其他问题");
        }
    }

    @Override
    public void onComplete() {

    }

    abstract void onStart(String s);
    abstract void onSuccess(T t);
    abstract void onFailure(String s);

}
